<?php

/**
 * BirthdayWidget class
 *
 * @author Julot
 * @since Thursday, June 7, 2012. 10:57 AM
 */
class BirthdayWidget extends CWidget {

	public $today = true;

	public function run() {
		$cmd = Yii::app()->db->createCommand();
		$cmd->select('id, name, birthDate');
		$cmd->from(Employee::model()->tableName());
		if (Yii::app()->db->driverName == 'mysql') {
			if ($this->today) {
				$where = "STR_TO_DATE(CONCAT_WS('-', YEAR (CURDATE()), ";
				$where .= "MONTH(birthDate), DAY(birthDate)), '%Y-%m-%d') ";
				$where .= "= CURDATE()";
			} else {
				$where = "STR_TO_DATE(CONCAT_WS('-', YEAR (CURDATE()), ";
				$where .= "MONTH(birthDate), DAY(birthDate)), '%Y-%m-%d') ";
				$where .= "BETWEEN ADDDATE(CURDATE(), INTERVAL 1 DAY) AND ";
				$where .= "ADDDATE(CURDATE(), INTERVAL 7 DAY)";
			}
			$cmd->where($where);
			$cmd->order('MONTH(birthDate), DAY(birthDate)');
		} else {
			$cmd->limit(5);
		}
		$employees = $cmd->queryAll();
		$this->render('birthday', array('data'=>$employees));
	}

}
